package com.gykfbxxh.imovie.custom.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.app.utils.util.ViewSizeUtils;


/**
 * create xuxinghai
 * date 161013
 * 配合viewpager顶部蓝条
 */
public class SimpleIndicator extends LinearLayout {

    private static final int COLOR_TEXT_SELECTOR = 0xFF007dce;

    private String[] mTitles;
    private int mTabCount;
    private int mIndicatorColor;
    private float mTranslationX;
    private Paint mPaint = new Paint();
    private int mTabWidth;

    public SimpleIndicator(Context context) {
        this(context, null);
    }

    public SimpleIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);

        mIndicatorColor = COLOR_TEXT_SELECTOR;
        mPaint.setColor(mIndicatorColor);
        mPaint.setStrokeWidth(ViewSizeUtils.dp2px(getContext(), 2));
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTabWidth = w / mTabCount;
    }

    public void setIndicatorCount(int count) {
        mTabCount = count;
    }

    public void setIndicatorColor(int indicatorColor) {
        this.mIndicatorColor = indicatorColor;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(mTranslationX, getHeight() - 2);
        canvas.drawLine(0, 0, mTabWidth, 0, mPaint);
        canvas.restore();
    }

    public void scroll(int position, float offset) {
        /**
         * <pre>
         *  0-1:position=0 ;1-0:postion=0;
         * </pre>
         */
        mTranslationX = getWidth() / mTabCount * (position + offset);
        invalidate();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

}
